﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class WeiXinNews
    {
        public int Add(Chain.Model.WeiXinNews model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into WeiXinNews(");
            builder.Append("NewsRuleID,NewsTitle,NewsDesc,NewsUrlFirst,NewsUrlSecond,NewsLinkContent,NewsCreateTime)");
            builder.Append(" values (");
            builder.Append("@NewsRuleID,@NewsTitle,@NewsDesc,@NewsUrlFirst,@NewsUrlSecond,@NewsLinkContent,@NewsCreateTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewsRuleID", SqlDbType.Int, 4), new SqlParameter("@NewsTitle", SqlDbType.NVarChar, 500), new SqlParameter("@NewsDesc", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@NewsUrlFirst", SqlDbType.NVarChar, 200), new SqlParameter("@NewsUrlSecond", SqlDbType.NVarChar, 200), new SqlParameter("@NewsLinkContent", SqlDbType.NVarChar, 0x7fffffff), new SqlParameter("@NewsCreateTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.NewsRuleID;
            cmdParms[1].Value = model.NewsTitle;
            cmdParms[2].Value = model.NewsDesc;
            cmdParms[3].Value = model.NewsUrlFirst;
            cmdParms[4].Value = model.NewsUrlSecond;
            cmdParms[5].Value = model.NewsLinkContent;
            cmdParms[6].Value = model.NewsCreateTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int NewsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinNews ");
            builder.Append(" where NewsID=@NewsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = NewsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string NewsIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinNews ");
            builder.Append(" where NewsID in (" + NewsIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int NewsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from WeiXinNews");
            builder.Append(" where NewsID=@NewsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = NewsID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select NewsID,NewsRuleID,NewsTitle,NewsDesc,NewsUrlFirst,NewsUrlSecond,NewsLinkContent,NewsCreateTime,RuleNUmber ");
            builder.Append(" FROM WeiXinNews,WeiXinRule ");
            builder.Append("where NewsRuleID=RuleID ");
            if (strWhere.Trim() != "")
            {
                builder.Append("and " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" NewsID,NewsRuleID,NewsTitle,NewsDesc,NewsUrlFirst,NewsUrlSecond,NewsCreateTime ");
            builder.Append(" FROM WeiXinNews ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.NewsID desc");
            }
            builder.Append(")AS Row, T.*  from WeiXinNews T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("NewsID", "WeiXinNews");
        }

        public Chain.Model.WeiXinNews GetModel(int NewsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 NewsID,NewsRuleID,NewsTitle,NewsDesc,NewsUrlFirst,NewsUrlSecond,NewsLinkContent,NewsCreateTime from WeiXinNews ");
            builder.Append(" where NewsID=@NewsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = NewsID;
            Chain.Model.WeiXinNews news = new Chain.Model.WeiXinNews();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["NewsID"] != null) && (set.Tables[0].Rows[0]["NewsID"].ToString() != ""))
            {
                news.NewsID = int.Parse(set.Tables[0].Rows[0]["NewsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["NewsRuleID"] != null) && (set.Tables[0].Rows[0]["NewsRuleID"].ToString() != ""))
            {
                news.NewsRuleID = int.Parse(set.Tables[0].Rows[0]["NewsRuleID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["NewsTitle"] != null) && (set.Tables[0].Rows[0]["NewsTitle"].ToString() != ""))
            {
                news.NewsTitle = set.Tables[0].Rows[0]["NewsTitle"].ToString();
            }
            if ((set.Tables[0].Rows[0]["NewsDesc"] != null) && (set.Tables[0].Rows[0]["NewsDesc"].ToString() != ""))
            {
                news.NewsDesc = set.Tables[0].Rows[0]["NewsDesc"].ToString();
            }
            if ((set.Tables[0].Rows[0]["NewsUrlFirst"] != null) && (set.Tables[0].Rows[0]["NewsUrlFirst"].ToString() != ""))
            {
                news.NewsUrlFirst = set.Tables[0].Rows[0]["NewsUrlFirst"].ToString();
            }
            if ((set.Tables[0].Rows[0]["NewsUrlSecond"] != null) && (set.Tables[0].Rows[0]["NewsUrlSecond"].ToString() != ""))
            {
                news.NewsUrlSecond = set.Tables[0].Rows[0]["NewsUrlSecond"].ToString();
            }
            if ((set.Tables[0].Rows[0]["NewsCreateTime"] != null) && (set.Tables[0].Rows[0]["NewsCreateTime"].ToString() != ""))
            {
                news.NewsCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["NewsCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["NewsLinkContent"] != null) && (set.Tables[0].Rows[0]["NewsLinkContent"].ToString() != ""))
            {
                news.NewsLinkContent = set.Tables[0].Rows[0]["NewsLinkContent"].ToString();
            }
            return news;
        }

        public DataTable GetParent()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            builder.Append("WeiXinRule.*,UserName,(select count(*) from WeiXinNews where NewsRuleID=RuleID) as NewsThisCount ");
            builder.Append("from WeiXinRule ,SysUser ");
            builder.Append("where RuleNewsType='news' ");
            builder.Append("and UserID=RuleUserID");
            return DbHelperSQL.Query(builder.ToString()).Tables[0];
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM WeiXinNews ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.WeiXinNews model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update WeiXinNews set ");
            builder.Append("NewsRuleID=@NewsRuleID,");
            builder.Append("NewsTitle=@NewsTitle,");
            builder.Append("NewsDesc=@NewsDesc,");
            builder.Append("NewsUrlFirst=@NewsUrlFirst,");
            builder.Append("NewsUrlSecond=@NewsUrlSecond,");
            builder.Append("NewsLinkContent=@NewsLinkContent,");
            builder.Append("NewsCreateTime=@NewsCreateTime");
            builder.Append(" where NewsID=@NewsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewsRuleID", SqlDbType.Int, 4), new SqlParameter("@NewsTitle", SqlDbType.NVarChar, 500), new SqlParameter("@NewsDesc", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@NewsUrlFirst", SqlDbType.NVarChar, 200), new SqlParameter("@NewsUrlSecond", SqlDbType.NVarChar, 200), new SqlParameter("@NewsLinkContent", SqlDbType.NVarChar, 0x7fffffff), new SqlParameter("@NewsCreateTime", SqlDbType.DateTime), new SqlParameter("@NewsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.NewsRuleID;
            cmdParms[1].Value = model.NewsTitle;
            cmdParms[2].Value = model.NewsDesc;
            cmdParms[3].Value = model.NewsUrlFirst;
            cmdParms[4].Value = model.NewsUrlSecond;
            cmdParms[5].Value = model.NewsLinkContent;
            cmdParms[6].Value = model.NewsCreateTime;
            cmdParms[7].Value = model.NewsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

